package com.chaodev.base.service.impl;


import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.dao.PlayGuideDao;
import com.chaodev.base.dao.CommDao;
import com.chaodev.base.dto.PlayGuideDto;
import com.chaodev.base.model.PlayGuide;
import com.chaodev.base.service.PlayGuideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PlayGuideServiceImpl implements PlayGuideService {

    @Autowired
    PlayGuideDao playGuideDao;
    @Autowired
    CommDao commDao;

    @Override
    public PlayGuide findDataById(String id) {
        return playGuideDao.findDataById(id);
    }

    @Override
    public void saveData(PlayGuide playGuide) throws DataAccessException {
        if(!StrUtil.isEmpty(playGuide.getId())){
            playGuideDao.updateDataById(playGuide);
        }else{
            playGuideDao.insertData(playGuide);

//            ApplyLc applyLc = new ApplyLc();
//            applyLc.setBizType(BizTypeEnum.Em_Article.getValue());
//            applyLc.setBizId(Integer.parseInt(playGuide.getId()));
//            applyLc.setReqState(1);
//            applyLc.setLcId("lc_001");
//            applyLc.setResultCode(OperEnum.Em_Add.getValue());
//            applyLc.setResultDsc(OperEnum.Em_Add.getName());
//            applyLc.setOpr(playGuide.getCUser());
//            applyLc.setOpTime(new Date());
//            applyLc.setState(2);
//            commDao.saveApply(applyLc);
//
//            ApplyStateToNext stateToNext = new ApplyStateToNext();
//            stateToNext.setBizType("1003");
//            stateToNext.setReqState(1);
//            stateToNext.setNowLcId("lc_001");
//            stateToNext.setOp(OperEnum.Em_Add.getValue());
//            ApplyStateToNext nextFlowInfo = commDao.getNextFlowInfo(stateToNext);
//
//            Task task = new Task();
//            task.setBizType(BizTypeEnum.Em_Article.getValue());
//            task.setTitle(playGuide.getTitle());
//            task.setBizId(Integer.valueOf(playGuide.getId()));
//            task.setLcId(nextFlowInfo.getNextLcId());
//            task.setLcName(nextFlowInfo.getNextLcName());
//            task.setCUser(playGuide.getCUser());
//            task.setState(1);
//            commDao.saveTask(task);
//
//            applyLc = new ApplyLc();
//            applyLc.setBizType(BizTypeEnum.Em_Article.getValue());
//            applyLc.setBizId(Integer.parseInt(playGuide.getId()));
//            applyLc.setReqState(1);
//            if(StrUtil.isNotEmpty(nextFlowInfo.getNextLcId())) {
//                applyLc.setLcId(nextFlowInfo.getNextLcId());
//            }
//            applyLc.setState(1);
//            commDao.saveApply(applyLc);
        }
    }

    @Override
    public void insertData(PlayGuide playGuide) throws DataAccessException {
        playGuideDao.insertData(playGuide);
    }

    @Override
    public void updateDataById(PlayGuide playGuide) throws DataAccessException {
        playGuideDao.updateDataById(playGuide);
    }

    @Override
    public void deleteDataById(String id) throws DataAccessException {
        playGuideDao.deleteDataById(id);
//        Task task = new Task();
//        task.setBizType(BizTypeEnum.Em_Article.getValue());
//        task.setDelId(id);
//        commDao.delTask(task);
    }

    @Override
    public List<PlayGuide> queryDataList(PlayGuideDto dto) {
        return playGuideDao.queryDataList(dto);
    }

    @Override
    public PageInfo<PlayGuide> queryPageList(PlayGuideDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getRows());
        List<PlayGuide> tbPlayGuideList = playGuideDao.queryDataList(dto);
        PageInfo<PlayGuide> pageInfo = new PageInfo<PlayGuide>(tbPlayGuideList, dto.getRows());
        return pageInfo;
    }

    @Override
    public void updateVisitNum(PlayGuide playGuide) {
        PlayGuide dataById = playGuideDao.findDataById(playGuide.getId() + "");
        Long visitNum = dataById.getVisitNum() == null ? 0 : dataById.getVisitNum();
        playGuide.setVisitNum(visitNum + 1);
        playGuideDao.updateVisitNum(playGuide);
    }

    @Override
    public PageInfo<PlayGuide> queryPageAllList(PlayGuideDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getRows());
        List<PlayGuide> tbPlayGuideList = playGuideDao.queryAllDataList(dto);
        PageInfo<PlayGuide> pageInfo = new PageInfo<PlayGuide>(tbPlayGuideList, dto.getRows());
        return pageInfo;
    }
    
}
